using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using Sedge.Core;
using Sedge.Core.App;
using Sedge.Core.Configuration;
using Sedge.Core.Execution;
using Sedge.Core.Execution.ReportElements;
using Sedge.Core.Globalization;

namespace Sedge.UI.ViewModel
{
	public class ScreenshotViewModel
	{
		#region Private fields

		private string _lastImagePath = String.Empty;

		private readonly Suite _suite;
		private readonly Options _options;

		#endregion //Private fields

		#region Constructor

		public ScreenshotViewModel(Suite suite, Options options)
		{
			_suite = suite;
			_options = options;
		}

		#endregion //Constructor

		#region Public Methods

		public Bitmap TakeScreenshot(Screen screen)
		{
			Bitmap bmp = new Bitmap(1, 1);
			Graphics g;
			string imagePath = String.Empty;
			try
			{
				bmp = new Bitmap(screen.Bounds.Width, screen.Bounds.Height, PixelFormat.Format32bppArgb);
				g = Graphics.FromImage(bmp);
				g.CopyFromScreen(screen.Bounds.X, screen.Bounds.Y, 0, 0, screen.Bounds.Size, CopyPixelOperation.SourceCopy);

				const string imageName = "Screenshot.png";
				imagePath = Services.FileSystem.Path.Combine(_suite.ImagesFolder, imageName);
				bmp.Save(imagePath, ImageFormat.Png);

				_suite.TempData[TempDataKeys.ScreenshotKey] = 
						new ReportImageList(_suite, String.Format("..\\{0}\\{1}", Branding.ReportImages, imageName));
				_lastImagePath = imagePath;
			}
			catch (Exception)
			{
				Services.Communications.Error(String.Format(Locale.Strings.MsgErrorFileSave, imagePath));
			}
			return bmp;
		}

		public void RemoveScreenshot()
		{
			if (!String.IsNullOrEmpty(_lastImagePath))
			{
				try
				{
					Services.FileSystem.File.Delete(_lastImagePath);
				}
				finally
				{
					_lastImagePath = String.Empty;
					_suite.TempData.Remove(TempDataKeys.ScreenshotKey);
				}
			}
		}

		#endregion //Public Methods
	}
}